package com.wsdcd.zlg.model.service;

import com.wsdcd.zlg.model.entity.Order;
import com.wsdcd.zlg.core.exception.ERPException;
import com.wsdcd.zlg.mapper.OrderMapper;
import com.wsdcd.zlg.util.PageParameter;
import com.wsdcd.zlg.util.PagerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class OrderServiceImpl implements OrderService {
	private final OrderMapper orderMapper;
	private final StatisticsService statisticsService;

	@Override
	public PagerUtil<Order> selectByPage(PageParameter pageParameter) {
		int count = orderMapper.getCount();
		List<Order> data= orderMapper.getPageList(pageParameter);
		return new PagerUtil<>(count, data);
	}

	@Override
	public void insert(Order order) throws ERPException {
		String orderUser = order.getOrderUser();
		if(orderUser == null || orderUser.trim().equals("")){
			throw new ERPException("没有选择订购客户");
		}
		String orderProducts = order.getOrderProducts();
		if(orderProducts == null || orderProducts.trim().equals("")){
			throw new ERPException("没有选择订购产品");
		}
		orderMapper.insert(order);
		statisticsService.orderNumberAdd();
	}

	@Override
	public Order selectByOrderId(String orderId) {
		return orderMapper.selectByOrderId(orderId);
	}

	@Override
	public void update(Order order) throws ERPException {
		double orderQuantity = order.getOrderQuantity();
		if(orderQuantity<0){
			throw new ERPException("数量不能小于0");
		}
		double unitPriceBeforeTax = order.getUnitPriceBeforeTax();
		if(unitPriceBeforeTax<0){
			throw new ERPException("单价不能小于0");
		}
		int orderId = order.getOrderId();
		Order oldOrder = orderMapper.selectByOrderId(orderId+"");
		if(order.equals(oldOrder)){
			throw new ERPException("修改后不能和原订单相同");
		}
		orderMapper.update(order);
	}

	@Override
	public void deleteAll(int[] orderId) {
		orderMapper.deleteAll(orderId);
	}

}
